package com.party.admin.web.controller.analysis;

import com.google.common.collect.Maps;
import com.party.admin.biz.analysis.StatisticalEventBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.common.CommonInput;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.analysis.StatisticsEvent;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.analysis.IStatisticsEventService;
import com.party.core.service.member.IMemberGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @program: Project
 * @description: 统计事件控制器
 * @author: Huaqunhao
 * @create: 2018-08-02 14:31
 **/
@Controller
@RequestMapping(value = "analysis/event")
public class StatisticalEventController {

    @Autowired
    private IStatisticsEventService iStatisticsEventService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private StatisticalEventBizService statisticalEventBizService;


    /**
     * @Description: 事件列表
     * @Param:
     * @return:
     * @Author: Huaqunhao
     * @Date: 2018/8/2/002
     */
//    @RequestMapping(value = "/list")
//    public ModelAndView list(Page page, StatisticsEvent se, CommonInput commonInput) {
//        page.setLimit(15);
//        ModelAndView mv = new ModelAndView("analysis/statisticalEventList");
//
//        CommonInput.transfromDate(commonInput);
//        Map<String, Object> params = Maps.newHashMap();
//        params.put("startDate", commonInput.getCreateStart());
//        params.put("endDate", commonInput.getCreateEnd());
//
//        if (StringUtils.isNotEmpty(commonInput.getCreateStart())) {
//            commonInput.setCreateStart(commonInput.getCreateStart().substring(0, "2018-05-04".length()));
//        }
//        if (StringUtils.isNotEmpty(commonInput.getCreateEnd())) {
//            commonInput.setCreateEnd(commonInput.getCreateEnd().substring(0, "2018-05-04".length()));
//        }
//
//        List<StatisticsEvent> list = iStatisticsEventService.webListPage(se, params, page);
//
//        mv.addObject("list", list);
//        mv.addObject("page", page);
//        mv.addObject("input", commonInput);
//        mv.addObject("se", se);
//        return mv;
//    }
    @RequestMapping(value = "/list")
    public ModelAndView list(Page page, StatisticsEvent se, CommonInput commonInput, String partnerId) {
        if (null == se.getType()) {
            se.setType(StatisticsEvent.TYPE_WEB);
        }
        page.setLimit(15);
        ModelAndView mv = new ModelAndView("analysis/statisticalEventCountList");
        if (commonInput.getTimeType() == null&&
                (StringUtils.isBlank(commonInput.getCreateEnd())||StringUtils.isBlank(commonInput.getCreateStart()))
                ){
            commonInput.setTimeType(2);
        }
        CommonInput.transfromDate(commonInput);
        Map<String, Object> params = Maps.newHashMap();
        params.put("startDate", commonInput.getCreateStart() + " 00:00:00");
        params.put("endDate", commonInput.getCreateEnd() + " 23:59:59");
        params.put("partnerId", partnerId);
        if (StringUtils.isNotEmpty(commonInput.getCreateStart())) {
            commonInput.setCreateStart(commonInput.getCreateStart().substring(0, 10));
        }
        if (StringUtils.isNotEmpty(commonInput.getCreateEnd())) {
            commonInput.setCreateEnd(commonInput.getCreateEnd().substring(0, 10));
        }
        List<StatisticsEvent> list = iStatisticsEventService.listPageByOthers(se, params, page);//所有事件日常访问统计表
        mv.addObject("list", list);
        mv.addObject("page", page);
        mv.addObject("input", commonInput);
        mv.addObject("se", se);
        if (StatisticsEvent.TYPE_MOBILE.equals(se.getType())) {
            List<MemberGroup> partnerList = memberGroupService.getAllPartnerWithName();
            mv.addObject("partnerList", partnerList);//回显到合作商下拉选择，建议做到缓存中
        }
        mv.addObject("partnerId", partnerId);
        return mv;
    }

    /**
     * @Description: 查看
     * @Param:
     * @return:
     * @Author: Huaqunhao
     * @Date: 2018/8/3/003
     */
    @RequestMapping(value = "/form")
    public ModelAndView form(StatisticsEvent se) {
        ModelAndView mv = new ModelAndView("analysis/statisticalEventForm");
        if (StringUtils.isNotBlank(se.getId())) {
            se = iStatisticsEventService.get(se.getId());
        }
        mv.addObject("se", se);
        return mv;
    }

    /**
     * @Description: 新增||编辑事件
     * @Param:
     * @return:
     * @Author: Huaqunhao
     * @Date: 2018/8/2/002
     */
    @ResponseBody
    @RequestMapping(value = "/saveOrupdate")
    public AjaxResult saveOrupdate(StatisticsEvent se, BindingResult result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result.hasErrors()) {
            List<ObjectError> allErros = result.getAllErrors();
            ajaxResult.setDescription(allErros.get(0).getDefaultMessage());
            return ajaxResult;
        }
        try {
            statisticalEventBizService.saveOrupdate(se);
            return new AjaxResult(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
        }
        return new AjaxResult(false);
    }


    /**
     * @Description: 删除事件
     * @Param:
     * @return:
     * @Author: Huaqunhao
     * @Date: 2018/8/2/002
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public AjaxResult delete(String id) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            iStatisticsEventService.delete(id);
            return new AjaxResult(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
        }
        return new AjaxResult(false);
    }

    /**
     * @Description: 校验事件
     * @Param:
     * @return:
     * @Author: Huaqunhao
     * @Date: 2018/8/2/002
     */
    @ResponseBody
    @RequestMapping(value = "/eventIsValid")
    public AjaxResult eventIsValid(StatisticsEvent statisticsEvent) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            StatisticsEvent seDB = iStatisticsEventService.getEvent(statisticsEvent);
            return new AjaxResult(seDB == null ? false : true);
        } catch (Exception e) {
            ajaxResult.setSuccess(true);
        }
        return new AjaxResult(true);
    }
}
